package cn.edu.hrbcu.es.invigilatemanagerment.service;

import cn.edu.hrbcu.es.invigilatemanagerment.dao.CourseMapper;
import cn.edu.hrbcu.es.invigilatemanagerment.pojo.Course;
import cn.edu.hrbcu.es.invigilatemanagerment.pojo.CourseExample;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements ICourseService{
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public List<Course> queryAllCourse() {
        CourseExample example = new CourseExample();
        CourseExample.Criteria criteria = example.createCriteria();
        criteria.andIdGreaterThan(0);

        return courseMapper.selectByExample(example);
    }

    @Override
    public Course queryCourseById(Integer id) {
        return courseMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer deleteCourseById(Integer id) {
        return courseMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer updateCourseById(Course course) {
        return courseMapper.updateByPrimaryKey(course);
    }

    @Override
    public Integer insertCourse(Course course) {
        return courseMapper.insert(course);
    }

    @Override
    public List<Course> queryCourseByName(String name) {
        CourseExample example = new CourseExample();
        CourseExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        return courseMapper.selectByExample(example);
    }

    @Override
    public int queryTotalCount() {
        CourseExample example = new CourseExample();
        CourseExample.Criteria criteria = example.createCriteria();
        criteria.getAllCriteria();
        return (int) courseMapper.countByExample(example);
    }

    @Override
    public List<Course> queryCourseByPage(int pageNow, int pageSize) {
//        PageHelper.startPage(pageNow, pageSize); 是用于启动分页功能的，但它只对紧跟在其后的第一个查询有效,所以要放在前面
        PageHelper.startPage(pageNow,pageSize);

        CourseExample example = new CourseExample();
        CourseExample.Criteria criteria = example.createCriteria();
        criteria.getAllCriteria();
        List<Course> courses = courseMapper.selectByExample(example);

        PageInfo<Course> coursePageInfo = new PageInfo<>(courses);
        return coursePageInfo.getList();
    }
}
